package com.lovi.admin.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * shiro 三大核心配置
 *
 */
@Configuration
public class ShiroConfig {
    //ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager")  DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        //关联
        factoryBean.setSecurityManager(defaultWebSecurityManager);

        Map<String, String> filterMap = new LinkedHashMap<>();
//        filterMap.put("/add","anon");
//        filterMap.put("/update","authc");
        // 授权过滤器
//        filterMap.put("/add","perms[add]");
        //设置shiro的过滤链
        factoryBean.setFilterChainDefinitionMap(filterMap);
        //让认证失败的跳转到登录页面
        factoryBean.setLoginUrl("/user/login");
        //未授权页面的设定
        factoryBean.setUnauthorizedUrl("/noauth");
        return factoryBean;
    }

    //DefaultSecurityManager
    @Bean(name="securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") MyUserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联realm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //Reaml 连接数据
    @Bean(name="userRealm")
    public MyUserRealm getRealm(){
        return new MyUserRealm();
    }


    // 配置ShiroDialect：方言，用于 thymeleaf 和 shiro 标签配合使用
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }

}
